﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using SpringStudio.Common;
using SpringStudio.DAL;

namespace Sdepci.InfoCode.BLL.Authority
{
    /// <summary>
    /// 角色类
    /// </summary>
    public class Role
    {
        #region 属性定义

        private string _roleGuid;
        /// <summary>
        /// 角色Guid
        /// </summary>
        public string RoleGuid
        {
            get { return this._roleGuid; }
        }

        private string _roleName;
        /// <summary>
        /// 角色名称
        /// </summary>
        public string RoleName
        {
            get { return this._roleName; }
        }

        private string _funcGuids;
        /// <summary>
        /// 角色权限
        /// </summary>
        public string FuncGuids
        {
            get { return this._funcGuids; }
        }

        private bool _defaultRole;
        /// <summary>
        /// 默认角色
        /// </summary>
        public bool DefaultRole
        {
            get { return this._defaultRole; }
        }

        private int _seq;
        /// <summary>
        /// 显示次序
        /// </summary>
        public int Seq
        {
            get { return this._seq; }
        }

        #endregion

        #region 构造方法

        public Role(string roleGuid)
        {
            this._roleGuid = roleGuid;

            try
            {
                string sql = "Select RoleName,FuncGuids,DefaultRole,Seq From InfoCodeRoles Where RoleGuid=@RoleGuid";
                DataSet ds = SqlHelper.ExecuteDataSet(ConnectionString.Default, CommandType.Text, sql,
                    SqlHelper.NewSqlParameter("@RoleGuid", this._roleGuid));
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    DataRow row = ds.Tables[0].Rows[0];
                    this._roleName = row["RoleName"].ToString();
                    this._funcGuids = row["FuncGuids"].ToString();
                    this._defaultRole = (bool)row["DefaultRole"];
                    this._seq = int.Parse(row["Seq"].ToString());
                }
                else
                {
                    throw new Exception("没有找到该值对应的详细记录！（参数：RoleGuid='" + this._roleGuid + "'）");
                }
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(ex);
            }
        }

        public Role(string roleGuid, string roleName, string funcGuids, bool defaultRole, int seq)
        {
            this._roleGuid = roleGuid;
            this._roleName = roleName;
            this._funcGuids = funcGuids;
            this._defaultRole = defaultRole;
            this._seq = seq;
        }

        #endregion
    }
}
